package com.pds.web.exit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.system.mapper.SysUserMapper;
import com.pds.web.exit.entity.PdsOrganizationExit;
import com.pds.web.exit.mapper.PdsOrganizationExitMapper;
import com.pds.web.exit.service.PdsOrganizationExitService;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 机构退出详情表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-06
 */
@Service("pdsOrganizationExitService")
public class PdsOrganizationExitServiceImpl extends ServiceImpl<PdsOrganizationExitMapper, PdsOrganizationExit> implements PdsOrganizationExitService {
    @Resource
    private PdsOrganizationExitMapper pdsOrganizationExitMapper;

    @Resource
    private PdsTestOrganizationInfoMapper pdsTestOrganizationInfoMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<PdsOrganizationExit> selectList(PdsOrganizationExit pdsOrganizationExit) {
        QueryWrapper<PdsOrganizationExit> wrapper = new QueryWrapper<>();
        Date exitStartDate = pdsOrganizationExit.getExitStartDate();
        Date exitEndDate = pdsOrganizationExit.getExitEndDate();
        String exitReason = pdsOrganizationExit.getExitReason();
        if(!Objects.isNull(exitStartDate)){
            wrapper.ge("exit_start_date",exitStartDate);
        }
        if(!Objects.isNull(exitEndDate)){
            wrapper.le("exit_end_date",exitEndDate);
        }
        if(!Objects.isNull(exitReason) && !exitReason.isEmpty()){
            wrapper.like("exit_reason",exitReason);
        }
        return pdsOrganizationExitMapper.selectList(wrapper);
    }

    @Override
    public PdsOrganizationExit queryById(Long id) {
        return pdsOrganizationExitMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsOrganizationExit insert(PdsOrganizationExit pdsOrganizationExit) {
        Long organizationId = pdsOrganizationExit.getOrganizationId();
        if(!Objects.isNull(organizationId)){
            testOrganizationInfoExit(organizationId);
            organizationPersonDisableLogin(organizationId);
            Date date = DateUtils.parseDate(DateUtils.getTime());
            /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
            pdsOrganizationExit.setCreateBy(nickName);*/
            pdsOrganizationExit.setCreateTime(date);
            pdsOrganizationExitMapper.insert(pdsOrganizationExit);
        }
        return pdsOrganizationExit;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsOrganizationExit pdsOrganizationExit) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        /*String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
        pdsOrganizationExit.setUpdateBy(nickName);*/
        pdsOrganizationExit.setUpdateTime(date);
        return pdsOrganizationExitMapper.updateById(pdsOrganizationExit) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsOrganizationExitMapper.deleteById(id) > 0;
    }

    private void testOrganizationInfoExit(Long organizationId){
        PdsTestOrganizationInfo pdsTestOrganizationInfo = new PdsTestOrganizationInfo();
        pdsTestOrganizationInfo.setId(organizationId);
        pdsTestOrganizationInfo.setStatus("4");
        pdsTestOrganizationInfoMapper.updateById(pdsTestOrganizationInfo);
    }

    private void organizationPersonDisableLogin(Long organizationId){
        PdsTestOrganizationInfo organizationInfo = pdsTestOrganizationInfoMapper.selectById(organizationId);
        Long deptId = organizationInfo.getDeptId();
        sysUserMapper.updateDepartmentUserStatus(deptId);
    }
}